<?php
namespace App\Service;
use App\Lib\Logger;
use Illuminate\Support\Facades\DB;
use App\Models\Admin\UserGroup as UserGroupModel;
use App\Models\Admin\UserGroupMember;
use App\Models\Admin\Post as PostModel;
use App\Models\Admin\PostGroup as PostGroupModel;
use App\Models\Admin\Org as OrgModel;
use App\Models\User as UserModel;
class UserGroupService extends BaseService{

    /*
     * 列表
     */
    public function getUserGroupList($search) {
        return UserGroupModel::getList(isset($search['limit'])?$search['limit']:15,$search);
    }
    /*
     * 创建单条数据
     */
    public function createData($data){
        $data['site_id'] = getCurrentSiteId();
        $result = UserGroupModel::create($data);
        if($result){
            $level = Logger::LOG_LEVEL_INFO;
            $message = "新增用户组[ID:".$result['id']."],成功";
        }
        $this->log($level,Logger::POST,'create',$message,$result);
        return $result;
    }
    /*
     * 创建人员数据
     */
    public function createUserData($data){
        $arr = [];
        $arr = explode("-",$data['member_id']);
        $data['member_id'] = $arr[1];
        $result = UserGroupMember::create($data);
        if($result){
            $level = Logger::LOG_LEVEL_INFO;
            $message = "新增用户组成员[ID:".$result['id']."],成功";
        }
        $this->log($level,Logger::POST,'create',$message,$result);
        return $result;
    }
    /*
     * 创建批量数据
     */
    public function createUser($data){
        $status = [];
        DB::beginTransaction();
        foreach ($data["value"] as $k=>$v){
            $arr[$k]["group_id"] = $data["group_id"];
            $arr[$k]["member_id"] = $v['value'];
            $arr[$k]["type"] = $v['type'];
        }
        foreach ($arr as $k=>$v){
            $result = $this->createUserData($v);
            if($result){
                $status[] = 1;
            }else{
                $status[] = -1;
            }
        }
        if(array_search(-1,$status)){
            DB::rollBack();
            return -1;
        }else{
            DB::commit();
            return 1;
        }
    }
    /*
     * 查询单条数据
     */
    public function findData($id){
        return UserGroupModel::find($id);
    }
    /*
     * 修改单条数据
     */
    public function update($id,$data){
        $result = UserGroupModel::where("id",$id)->update($data);
        if($result){
            $level = Logger::LOG_LEVEL_INFO;
            $message = "修改用户组[ID:".$id."],成功";
        }
        $this->log($level,Logger::POST,'update',$message,$data);
        return $result;
    }
    /*
     * 删除单条数据
     */
    public function delete($id){
        $result = UserGroupModel::where("id",$id)->delete();
        if($result){
            $level = Logger::LOG_LEVEL_INFO;
            $message = "删除成功";
        }
        $this->log($level,Logger::POST,'destroy',$message,['del_id'=>$id]);
        return $result;
    }
    /*
     * 查询分组内数据
     */
    public function getUserList($search){
        return UserGroupMember::getList(isset($search['limit'])?$search['limit']:15,$search);
    }
    /*
     * 组成create数据
     */
    public function getData($model,$type,$group_id){
        $site_id = getCurrentSiteId();
        if($type == "post"){
            $ids = PostGroupModel::pluck('id');
            $post = $model::whereIn("group_id",$ids)
                ->whereNotIn('id',function($query) use ($type,$group_id){
                    $query->where('type',$type)->where('group_id',$group_id)->select('member_id')->from('user_group_member');
                })
                ->get();
        }else{
            $post = $model::whereNotIn('id',function($query) use ($type,$group_id){
                $query->where('type',$type)->where('group_id',$group_id)->select('member_id')->from('user_group_member');
            })->where("site_id",$site_id)->where(function($query) use ($type){
                if($type == "user"){
                    $query->where('is_locked',0);
                }
            })->get();
        }
        $arr = [];
        $i = 0;
        foreach ($post as $k=>$v){
            $arr[$i]['showname'] = $type == "user"?$v['username']:$v['name'];
            $arr[$i]['value'] = $type."-".$v['id'];
            $arr[$i]['type'] = $type;
            switch ($type){
                case "post":
                    $arr[$i]['name'] = '<i class="layui-icon layui-icon-group"></i>&nbsp;&nbsp;'.$v['name'];break;
                case "org":
                    $arr[$i]['name'] = '<i class="layui-icon layui-icon-user "></i>&nbsp;&nbsp;'.$v['name'];break;
                case "user":
                    $arr[$i]['name'] = '<i class="layui-icon layui-icon-username"></i>&nbsp;&nbsp;'.$v['username'];break;
            }
            $i++;
        }
        return $arr;
    }
    /*
     * 获取create控件数据并组成一个数组
     */
    public function getUserData($group_id){
        $post = $this->getData(new PostModel(),"post",$group_id);
        $org = $this->getData(new OrgModel(),"org",$group_id);
        $user = $this->getData(new UserModel(),"user",$group_id);
        $arr = array_merge($post, $org, $user);
        return json_encode($arr);
    }
    /*
    *  移除用户组人员
    */
    public function deleteUser($id){
        $result = UserGroupMember::where("id",$id)->delete();
        if($result){
            $level = Logger::LOG_LEVEL_INFO;
            $message = "删除成功";
        }
        $this->log($level,Logger::POST,'destroy',$message,['del_id'=>$id]);
        return $result;
    }
    public function selectData($where = []){
        return UserGroupMember::where($where)->get();
    }
}
